package com.ds.app;

import com.ds.org.Org;
import com.ds.org.Person;
import com.ds.org.PersonNotFoundException;
import com.ds.org.Role;
import com.ds.server.eumus.ConfigCode;

import java.util.ArrayList;
import java.util.List;

public class DbAppManager  implements AppManager{
    private ConfigCode subSystemId;
    private AppCacheManager appCacheManager;


    DbAppManager(){

    }

    @Override
    public void init(ConfigCode subSystemId) {

        this.subSystemId=subSystemId;
        this.appCacheManager= AppCacheManager.getInstance(subSystemId);
    }

    @Override
    public List<App> getAppList() {

        return    appCacheManager.getAppList();
    }

    @Override
    public List<App> getTopApps() {
        return appCacheManager.getTopApps();
    }

    @Override
    public List<Module> getModuleList() {
        List<Module> modules=new ArrayList<Module>();
        List<String> moduleIds=appCacheManager.getModuleIdList();;
        for (String  moduleId:moduleIds){
            try {
                modules.add(this.getModuleByID(moduleId));
            } catch (ModuleNotFoundException e) {
                e.printStackTrace();
            }
        }
        return modules;
    }

    @Override
    public App getAppByID(String appId) throws AppNotFoundException {
        return appCacheManager.getAppByID(appId);
    }

    @Override
    public Module getModuleByServiceID(String serviceId) throws ModuleNotFoundException {
        return appCacheManager.getModuleByServiceID(serviceId);
    }

    @Override
    public Module getModuleByID(String moduleId) throws ModuleNotFoundException {
        return appCacheManager.getModuleByID(moduleId);
    }

    @Override
    public List<Role> getRoleByModuleId(String moduleId) throws ModuleNotFoundException {
        return getModuleByID(moduleId).getRoleList();
    }

    @Override
    public List<Person> getPersonByModuleId(String moduleId) throws ModuleNotFoundException {
        return getModuleByID(moduleId).getAllRightPerson();
    }


    @Override
    public List<Role> getRoleByAppId(String moduleId) throws ModuleNotFoundException {
        return null;
    }

    @Override
    public List<Person> getPersonByAppId(String moduleId) throws ModuleNotFoundException {
        return null;
    }


    @Override
    public void addModule2Person(String personId, String moduleId) throws ModuleNotFoundException, PersonNotFoundException {
        throw new ModuleNotFoundException();
    }

    @Override
    public void removeModule4Person(String personId, String moduleId) throws ModuleNotFoundException, PersonNotFoundException {
        throw new ModuleNotFoundException();
    }

    @Override
    public void suspendModule(String personId, String moduleId) throws ModuleNotFoundException, PersonNotFoundException {
        throw new ModuleNotFoundException();
    }

    @Override
    public void resumeModule(String personId, String moduleId) throws ModuleNotFoundException, PersonNotFoundException {
        throw new ModuleNotFoundException();
    }
}
